<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2017-10-08
 * Time: 16:51
 */

?>

<div class="form-horizontal">
    <div class="form-group">
        <input type="hidden" id="agent_id" value="<?= isset($item) ? $item['id'] : '' ?>">
        <label class="col-lg-3 col-sm-3 control-label">名称：</label>
        <div class="col-lg-9">
            <input type="text" class="form-control" id="agent_name" name="agent_name"
                   value="<?= $item == null ? '' : $item['agent_name'] ?>"/>
        </div>
    </div>
    <div class="form-group">
        <label class="col-lg-3 col-sm-3 control-label">省：</label>
        <div class="col-lg-9">
            <select class="form-control" id="agent_province">
                <option>请选择</option>
                <?php
                if (isset($item)) {
                    if (isset($item['province_id'])) {
                        $provinces = \app\models\Area::getAreaByLevel(1);
                        foreach ($provinces as $province) {
                            $str_select = '';
                            if ($province['id'] == $item['province_id']) {
                                $str_select = 'selected="selected"';
                            }
                            ?>
                            <option value="<?= $province['id'] ?>" <?= $str_select ?>><?= $province['area_name'] ?></option>
                            <?php
                        }
                    }
                }
                ?>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-lg-3 col-sm-3 control-label">市：</label>
        <div class="col-lg-9">
            <select class="form-control" id="agent_city">
                <option>请选择</option>
                <?php
                if (isset($item)) {
                    if (isset($item['city_id'])) {
                        $cities = \app\models\Area::getAreaByLevel(2);
                        foreach ($cities as $city) {
                            $str_select = '';
                            if ($city['id'] == $item['city']) {
                                $str_select = 'selected="selected"';
                            }
                            ?>
                            <option value="<?= $city['id'] ?>" <?= $str_select ?>><?= $city['area_name'] ?></option>
                            <?php
                        }
                    }
                }
                ?>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-lg-3 col-sm-3 control-label">区：</label>
        <div class="col-lg-9">
            <select class="form-control" id="agent_county">
                <option>请选择</option>
                <?php
                if (isset($item)) {
                    if (isset($item['county_id'])) {
                        $counties = \app\models\Area::getAreaByLevel(3);
                        foreach ($counties as $county) {
                            $str_select = '';
                            if ($county['id'] == $item['county_id']) {
                                $str_select = 'selected="selected"';
                            }
                            ?>
                            <option value="<?= $county['id'] ?>" <?= $str_select ?>><?= $county['area_name'] ?></option>
                            <?php
                        }
                    }
                }
                ?>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-lg-3 col-sm-3 control-label">是否启用：</label>
        <div class="col-lg-9">
            <select class="form-control" id="is_enable">
                <?php
                $str_is_enabled_y = '';
                $str_is_enabled_n = '';
                if (isset($item)) {
                    if (isset($item['is_enabled'])) {
                        if ($item['is_enabled']) {
                            $str_is_enabled_y = 'selected="selected"';
                        } else {
                            $str_is_enabled_n = 'selected="selected"';
                        }
                    }
                }
                ?>

                <option value="1" <?= $str_is_enabled_y ?>>是</option>
                <option value="0" <?= $str_is_enabled_n ?>>否</option>
            </select>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(function () {
        initAreaByLevel(1, $("#agent_province"));
        $("#agent_province").change(function () {
            var province_id = $(this).children('option:selected').val();
            if (province_id) {
                initAreaByParentId(province_id, $("#agent_city"));
                emptyAreaSelect($("#agent_city"));
                emptyAreaSelect($("#agent_county"));
            }
        });
        $("#agent_city").change(function () {
            var city_id = $(this).children('option:selected').val();
            if (city_id) {
                initAreaByParentId(city_id, $("#agent_county"));
                emptyAreaSelect($("#agent_county"));
            }
        });
    });

    function initAreaByLevel(level, obj) {
        var data = {};
        data.level = level;

        requestJson('get-area-by-level-handler', data, true, function (responseData) {
            if (responseData.status == 0) {
                var items = responseData.data;
                for (var i = 0; i < items.length; i++) {
                    obj.append("<option value='" + items[i]["id"] + "'>" + items[i]['area_name'] + "</option>");
                }
            }
        });
    }

    function initAreaByParentId(parent_id, obj) {
        var data = {};
        data.parent_id = parent_id;
        requestJson('get-area-by-parent-id-handler', data, true, function (responseData) {
            if (responseData.status == 0) {
                var items = responseData.data;
                for (var i = 0; i < items.length; i++) {
                    obj.append("<option value='" + items[i]["id"] + "'>" + items[i]['area_name'] + "</option>");
                }
            }
        });
    }

    function emptyAreaSelect(obj) {
        obj.empty();
        obj.append("<option>请选择</option>");
    }

</script>